﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WidgetsDesign.API.Models;
using WidgetsDesign.API.Services;

namespace WidgetsDesign.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RechargeController : ControllerBase
    {
        private readonly IRechargeRepository _rechargeRepository;
        private readonly IUserRepository _userRepository;
        public RechargeController(IRechargeRepository rechargeRepository, IUserRepository userRepository)
        {
            _rechargeRepository = rechargeRepository;
            _userRepository = userRepository;
        }

        public class RechargeCardModel
        {
            public double Value { get; set; }
            public DateTime CreateTime { get; set; }
        }
        /// <summary>
        /// 创建充值卡
        /// </summary>
        /// <param name="rechargeCardModel"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        [HttpPost("{p}")]
        public ActionResult CreateRechargeCode([FromBody] RechargeCardModel rechargeCardModel, [FromRoute] int p)
        {
            if (p == 12138)
            {
                var list = new RechargeCard()
                {
                    Key = Guid.NewGuid().ToString(),
                    Value = rechargeCardModel.Value,
                    CreateTime = rechargeCardModel.CreateTime
                };
                _rechargeRepository.CreateRechargeCode(list);
                return Ok(list.Key);
            }
            return Ok();
        }
        public class ConversionModel
        {
            public string Key { get; set; }
            public int UserId { get; set; }
        }
        /// <summary>
        /// 兑换充值卡
        /// </summary>
        /// <param name="conversionModel"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Conversion([FromBody] ConversionModel conversionModel)
        {
            var card = await _rechargeRepository.GetRechargeCode(conversionModel.Key);
            if (card == null)
            {
                return Ok(new { code = 0, message = "兑换码错误，请认真核对后再次输入！" });
            }

            if (await _rechargeRepository.ExistUseRechargeCode(conversionModel.Key))
            {
                return Ok(new { code = 0, message = "该兑换码已使用" });
            }

            var user = await _userRepository.GetUserInfoById(conversionModel.UserId);
            if (user == null)
            {
                return Ok(new { code = 0, message = "不存在该用户" });
            }
            _userRepository.UpdateWallet(conversionModel.UserId, card.Value);
            await _userRepository.SaveAsync();
            var list = new RechargeRecord()
            {
                Key = conversionModel.Key,
                Value = card.Value,
                Uuid = conversionModel.UserId,
                ConversionTime = DateTime.Now
            };
            _rechargeRepository.Conversion(list);
            return Ok(new { code = 200, message = "兑换成功" });
        }
    }
}
